const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const src = "https://pet.51appdevelop.com";
//请求数据
const ajax = function (url, config, success, err) {
  let opt = {
    url: src + url,
    data: config.data || "",
    method: !config.method ? 'GET' : config.method.toUpperCase(),
    header: config.header || {},
    dataType: config.datatype || 'json',

    success: function (res) {
      // console.log(res);
      if (res.statusCode == 401) {
        // wx.navigateTo({
        //   url: '/pages/empower/empower'
        // })
      } else if (res.statusCode == 422) {
        wx.showToast({
          title: '参数错误',
          icon: 'none'
        })
      } else if (res.statusCode == 200) {
        success && success.call(this, res.data);
      }
    },
    fail: function (e) {
      console.debug(e);
      // fail && fail.call(this, e);
      // wx.showToast({
      //   title: '网络错误',
      //   icon: 'none'
      // })
    }
  }
  wx.request(opt)
}
module.exports = {
  formatTime: formatTime,
  src: src,
  ajax: ajax
}
